//
// Created by zhujin zhujin on 2017/1/21.
//

#include "checksignkey.h"
#include <jni.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <jni.h>
#include "base64.h"

//解密
char* mengchuang_decrypt(const char * tempdata) {
    char* MCHENCRYPTIONKEY = "gnauhcgnem";
    int i = strlen(tempdata);
    uint8 temppsd[20];
    int j = strlen(MCHENCRYPTIONKEY);
    int m = 0;
    uint8* outstr;
    int templen = 0;
    int start = 0;
    memset(outstr, 0, i);
    memset(temppsd, 0, 20);
    memcpy(temppsd, MCHENCRYPTIONKEY, j);
    templen = base64_decode((const uint8*) tempdata, strlen(tempdata),
                            (uint8*) outstr);
    while (start < templen) {
        outstr[start] = outstr[start] ^ temppsd[m >= j ? m = 0 : m++];
        start++;
    }
    return (char*) outstr;
}

extern "C"
jstring
Java_com_example_sdk_test_demo_jnitest_TestActivity_stringFromJNI(JNIEnv* env, jobject thiz,
                                                    jstring str) {
//    std::string hello = "Hello from C++";
//    return env->NewStringUTF(hello.c_str());
    const char *strt = (env)->GetStringUTFChars(str, 0);
    char* tempchar = mengchuang_decrypt(strt);
    jstring outstr = (env)->NewStringUTF(tempchar);
    free(tempchar);
    return outstr;

}

